package com.example.demo.controller;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.pojo.CssBean;

@RestController
public class HelloController {

    Logger logger = LoggerFactory.getLogger(HelloController.class);
    
	@RequestMapping("/")
	public String hello() {
		return "spring boot hello";
	}
	
	@RequestMapping(value="/rest",method={RequestMethod.GET,RequestMethod.POST})
	public CssBean SimpleRest(@RequestParam String session, 
			@RequestParam String handler, 
			@RequestParam String bean, 
			@RequestParam String msg) {
		//方法一，缺点是必须列出4个参数
		//http://localhost:8080/rest?session=1&handler=2&bean=3&msg=4
		CssBean cssBean = new CssBean(session,handler,bean,msg);
		return cssBean;
	}
	
	@RequestMapping(value = "/send", method = {RequestMethod.POST,RequestMethod.GET})
	public String Function(HttpServletRequest request, HttpServletResponse response) {
		//方法二，相同参数和不定参数的演示
		//http://localhost:8080/send?session=1&h=2&msg=4&msg=5&msg=6
        logger.info("模拟消息发送 begin");
        Map<String, String[]> parameterMap = request.getParameterMap();
        for(Entry<String, String[]> entry:parameterMap.entrySet()) {
        	logger.info(entry.getKey());
        	logger.info(entry.getValue()[0]);//获取第一个参数值
            //String[] 保存多个参数，例如：?a=aaa&a=bbb
//            logger.info(Arrays.toString(entry.getValue()));
//            for(String s:entry.getValue()) {
//            	logger.info(s);
//            }
        }
		return "success";
	}
}
